function [AR] = FindAspectRatio(data)
%% Written by Paul Taufalele
% Purpose: utilize the fourier transform to determine the anisotropy of
% confocal reflectance images of collagen hydrogels.
% Implemented as part of a matlab GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% folder = uigetdir();
% 
% File = dir([folder '/*.lsm']);
% NF = size(File,1);

%% Analyze each image

    %% Fourier Transform
    % Take the fourier transform of the raw image, then find the power spectrum
    % and save the power spectrum under the variable "Log" (worst name ever).
    Y = fft2(data);
    shF = fftshift(Y);
    power_spectrum = log(abs(shF.^2));
    power_spectrum(512:514,512:514)=0; % set the midpoints to 0 to avoid skewing the results
    filtered_power = medfilt2(power_spectrum, [25,25]);
%     figure
%     imagesc(filtered_power)
%     grid on
%     pbaspect([1 1 1]);
%     figure
   


    %% Estimating the ellipse based off power intensity
    maxA = max(max(filtered_power));
    eA=maxA *.77;
    reA = round(eA);
%     x = 1:1024;
%     y = 1:1024;
%     [X,Y] = meshgrid(x,y);
%     [Ca] = contour(X,Y,filtered_power,[eA,eA],'red');
     [r c] = find(round(filtered_power) == reA);
%      figure
%      scatter(r,c)
%     ellipse = fit_ellipse(Ca(1, 2:end), Ca(2, 2:end));
    ellipse = fit_ellipse(r, c);
    if isempty(ellipse.long_axis)
        AR = 0;
    else
        AR = ellipse.long_axis/ellipse.short_axis;
    end
    
    
